// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults 

var geocoder;
var map;
function initMapItinerary(){	
	var latlng = new google.maps.LatLng(document.getElementById('latitude').value,document.getElementById('longitude').value);
	var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
	
	map = new google.maps.Map(document.getElementById('map'), myOptions);
	
	var marker = new google.maps.Marker({
      position: latlng, 
      map: map
  });
  
  marker.setTitle(document.getElementById('location').value);
}

function codeItineraryAddress() {
	geocoder = new google.maps.Geocoder();
	var address = document.getElementById('tri_country').value + ' ' + document.getElementById('tri_city').value + ' ' + 
							document.getElementById('iti_location').value ;
    if (geocoder) {
      geocoder.geocode( { 'address': address}, 
		function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				document.getElementById('latitude').value = results[0].geometry.location.lat();														
				document.getElementById('longitude').value = results[0].geometry.location.lng();
				document.forms["flickr"].submit();
			} else {
				alert("Geocode was not successful for the following reason: " + status);
			}
		});
    }	
}
  
function initMapTrip(){
	var latlng;
	var trip_count = document.getElementById('trip_count').value;
	
	if (trip_count != 0){
		for (i=1;i<=trip_count;i=i+1){
			latlng = new google.maps.LatLng(document.getElementById('latitude'+i).value,document.getElementById('longitude'+i).value);
			
			var myOptions = {
				zoom: 8,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			
			if (i == 1){
				map = new google.maps.Map(document.getElementById('map'), myOptions);
			}
			
			var marker = new google.maps.Marker({
				position: latlng, 
				map: map
			});
		  
			marker.setTitle(document.getElementById('location'+i).value);
		}
	}
	else{
		geocoder = new google.maps.Geocoder();
		var myOptions = {
				zoom: 8,
				mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		map = new google.maps.Map(document.getElementById('map'), myOptions);
		var address = document.getElementById('tri_country').value + ' ' + document.getElementById('tri_city').value;
		if (geocoder) {
			geocoder.geocode( { 'address': address}, 
				function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						map.setCenter(results[0].geometry.location);
						 var marker = new google.maps.Marker({
												map: map, 
												position: results[0].geometry.location
											});
						marker.setTitle(document.getElementById('tri_country').value + ' ' +
								document.getElementById('tri_city').value);
					} else {
						alert("Geocode was not successful for the following reason: " + status);
					}
				});
		}
	}
	
}  